/**
 * Package:com.goldcitynet.util;
 * $Id: ExperienceCalculator.java,v 1.1.1.1 2009/03/23 09:21:18 zhangpei Exp $
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.util;

import java.util.List;
import java.util.ArrayList;

/**
 * ExperienceCalculator
 * <p/>
 * 经验值换算器，用来换算经验值，等级之间的关系
 * <p><a href="ExperienceCalculator.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author: zhangpei $</a>
 * @version $Revision: 1.1.1.1 $
 */
public class ExperienceCalculator {
    public static Float EXP_RATE = 0.2f;
    public static Integer EXP_BASE = 1000;
    public static List<Integer> EXP_RANK_MAP = new ArrayList<Integer>();
    /*暂时开放到70级*/
    static{
        for(int i=0; i<70;i++){
            EXP_RANK_MAP.add(i,(int)(EXP_BASE*Math.pow((1+EXP_RATE),i)));
        }
    }

    /**
     * 将经验值转换为等级,目前按照等比递增，即上一级需要的经验值比前一级多一定不百分比
     * @param exp
     * @return
     */
    public static Integer expToRank(Integer exp) {
        for(int i=1; i<EXP_RANK_MAP.size();i++){
            if(exp<EXP_RANK_MAP.get(i)&&exp>EXP_RANK_MAP.get(i-1))return i;
        }
        return 1;
    }

    /**
     * 根据当前经验值，返回当前级别所需经验值
     * @param exp
     * @return
     */
    public static Integer expForRankUp(Integer exp){
        return EXP_RANK_MAP.get(expToRank(exp))-EXP_RANK_MAP.get(expToRank(exp)-1);
    }

    /**
     * 根据总经验值，返回本级别已有经验值
     * @param exp
     * @return
     */
    public static Integer expForRankNow(Integer exp){
        return exp-EXP_RANK_MAP.get(expToRank(exp)-1);
    }


    public static void main(String[] args) {
        System.out.println("expToRank:"+expToRank(130));
        System.out.println("expToRank:"+expToRank(1300));
        System.out.println("expToRank:"+expToRank(2300));
        System.out.println("expToRank:"+expToRank(23000));
        System.out.println("expForRankUp:"+expForRankUp(3));
        System.out.println("expForRankNow:"+expForRankNow(1300));
    }
}
